package com.xianyun.site.support.executor.commond;

import com.alibaba.cloud.spring.boot.sms.ISmsService;
import com.alibaba.cola.exception.BizException;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.xianyun.site.support.dto.SmsReceiveCmd;
import com.xianyun.site.support.executor.handle.sms.SmsStrategyContent;
import com.xianyun.site.support.gatewayimpl.database.SmsConfigMapper;
import com.xianyun.site.support.gatewayimpl.database.domainobject.SmsConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author mac_zyj
 */
@Slf4j
@Component
public class SmsReceiveCmdExo {
    @Autowired
    private ISmsService smsService;

    @Autowired
    private SmsConfigMapper configMapper;


    public SendSmsResponse execute(SmsReceiveCmd smsReceiveCmd){
        SmsStrategyContent smsStrategyContent = new SmsStrategyContent(smsReceiveCmd.getCheckParam().getUserAction());
        SendSmsRequest sendSmsRequest= smsStrategyContent.initSendSmsRequest(smsReceiveCmd);
        try {
            SmsConfig smsConfig = configMapper.selectSmsConfigByConfigName(smsReceiveCmd.getSmsMessage().getConditionName());
            if (smsConfig.isOpen()){
                log.info("发送成功----"+smsReceiveCmd.getSmsMessage().getPhoneNumber());
                return smsService.sendSmsRequest(sendSmsRequest);
            }
        } catch (BizException e)
        {
            log.warn("BizException with error code:{},error message:{}",e.getErrCode(),e.getMessage());
        }
        catch (Exception ex) {
            log.error("System error"+ex.getMessage(),ex);
        }
        return new SendSmsResponse();
    }
}
